import 'package:flustars_flutter3/flustars_flutter3.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:synchronized/synchronized.dart';

/**
 * 资源初始化
 */
class ResInitUtil{
  static ResInitUtil _singleton;
  static Lock _lock = Lock();

  static ResInitUtil get singleton => _singleton??ResInitUtil._();

  static Future<ResInitUtil> getInstance() async {
    if (_singleton == null) {
      await _lock.synchronized(() async {
        if (_singleton == null) {
          // keep local instance till it is fully initialized.
          // 保持本地实例直到完全初始化。
          var singleton = ResInitUtil._();
          await singleton._init();
          _singleton = singleton;
        }
      });
    }
    return _singleton;
  }

  ResInitUtil._();

  Future _init() async {
  }
  //设置大小
  double setSize(double dp, {BuildContext context}) {
    return ScreenUtil.getAdapterSizeCtx(context ?? Get.context, dp);
  }
  //获取资源文件路径
  String getAssetsPath(String assetsName, {BuildContext context,String preUrl,String assetsType:'.png'}) {
    final assets = "assets/";
       final assets_images = "assets/images/";
      final png = ".png";
      final jpg = ".jpg";
      final jpeg = ".jpeg";
      final svg = ".svg";
      final html = ".html";
    if(assetsType==null)return'';
    if(assetsType.endsWith('.png'))return "${preUrl??assets_images}${assetsName}${png}";
    if(assetsType.endsWith('.jpg'))return "${preUrl??assets_images}${assetsName}${jpg}";
    if(assetsType.endsWith('.jpeg'))return "${preUrl??assets_images}${assetsName}${jpeg}";
    if(assetsType.endsWith('.svg'))return "${preUrl??assets_images}${assetsName}${svg}";
    if(assetsType.endsWith('.html'))return "${preUrl??assets}${assetsName}${html}";
    return '';
  }
}